package service

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"zsc-boot/app/message/model"
	"zsc-boot/app/system_layer/service"
	"zsc-boot/core/orm"
)

func SendMessage(userID int, content string, tx *gorm.DB, ctx *gin.Context) error {
	// 获取发送人
	var senderId = 0
	if ctx != nil {
		senderId = service.GetUserID(ctx)
	}

	if tx == nil {
		tx = orm.GetDb()
	}

	if err := tx.Model(&model.Message{}).Create(&model.Message{
		SenderId:   senderId,
		Content:    content,
		ReceiverId: userID,
	}).Error; err != nil {
		return err
	}
	return SendUserMsg(userID, content, Web, "msg")
}
